/*
 * File: exercise2.cpp
 * -------------------
 * trim function : trim1 is wrong, trim2 is right. 
 * compile command :
 * g++ -Wall -g exercise2.cpp -o exercise2 -lStanfordCPPLib -I ../include/
 */

#include <iostream>
#include <string>
using namespace std;

string trim1(string str);
string trim2(string str);

int main()
{
	string str = "Hello Archlinux. T O   T ";
	cout<<"Results1: "<<trim1(str)<<endl;
	cout<<"Results2: "<<trim2(str)<<endl;
	return 0;
}


string trim1(string str)
{
	int finish = str.length() - 1;
	while (finish >= 0 && isspace(str[finish])) 
	{
		finish--;
	}
	int start = 0;
	while (start <= finish && isspace(str[start]))
	{
		start++;
	}
	return str.substr(start, finish - start + 1);
}

string trim2(string str)
{
	int nchars = str.length();
	string newstr;
	for(int i = 0; i < nchars; i++)
	{
		if(str[i] != ' ')
			newstr += str[i];
	}
	return newstr;
}

